﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace PhotoGallery.Models.Utils
{
    public static class FileHelper
    {

        public static void SaveResizedImage(string filePath, string filename, string resizedFilename, int maxWidth = 180, int maxHeight = 180)
        {
            SaveResizedImage(Path.Combine(filePath, filename), Path.Combine(filePath, resizedFilename), maxWidth, maxHeight);
        }
        public static void SaveResizedImage(string fullFilePath, string resizedFileFullPath, int maxWidth = 180, int maxHeight = 180)
        {
            // load image
            using (Image srcImage = Image.FromFile(fullFilePath))
            {
                // crop image
                int width = srcImage.Width;
                int height = srcImage.Height;

                int iStart;
                Decimal divider = 1;
                int xcrop = 0;
                int ycrop = 0;
                bool portrait = (width < height);
                if (portrait)
                {
                    iStart = height;
                    divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                    ycrop = (height - width) / 2;
                    height = height - 2 * ycrop;

                }
                else
                {
                    iStart = width;
                    divider = Math.Abs((Decimal)iStart / (Decimal)maxWidth);
                    xcrop = (width - height) / 2;
                    width = width - 2 * xcrop;
                }
                Rectangle cropArea = new Rectangle(xcrop, ycrop, width, height);
                using (Image tmpImage = cropImage(srcImage, cropArea))
                {

                    // resize image
                    Size size = new Size(maxWidth, maxHeight);
                    using (Image resizedImg = resizeImage(tmpImage, size))
                    {

                        resizedImg.Save(resizedFileFullPath, ImageFormat.Jpeg);
                    }
                }
            }
        }

        public static void CreateResizedImage(string filePath, string filename, string resizedFilename, int maxWidth = 180, int maxHeight = 180)
        {
            CreateResizedImage(Path.Combine(filePath, filename), Path.Combine(filePath, resizedFilename), maxWidth, maxHeight);
        }
        public static void CreateResizedImage(string fullFilePath, string resizedFileFullPath, int maxWidth = 180, int maxHeight = 180)
        {
            // load image
            using (Image srcImage = Image.FromFile(fullFilePath))
            {
                // resize image
                Size size = new Size(maxWidth, maxHeight);
                using (Image resizedImg = resizeImage(srcImage, size))
                {
                    resizedImg.Save(resizedFileFullPath, ImageFormat.Jpeg);
                }
            }
        }

        private static Image cropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
            bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        private static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }        
    }
}